package com.lgy.ms.core.utils;

import com.lgy.ms.core.dto.SysUserDTO;

/**
 * @author Gylin
 */
public class EncryptUtil {

    public static final int SALT_SIZE = 8;
    public static final int HASH_INTERATIONS = 1024;

    /**
     * 设定安全的密码，生成随机的salt并经过1024次 sha-1 hash
     */
//    public static void encryptPassword(User user, String password) throws Exception {
//        byte[] salt = DigestUtils.generateSalt(SALT_SIZE);
//        user.setSalt(EncodesUtils.encodeHex(salt));
//        byte[] hashLoginPwd = DigestUtils.sha1(password.getBytes("UTF-8"), salt,
//                HASH_INTERATIONS);
//        user.setPassword(EncodesUtils.encodeHex(hashLoginPwd));
//    }
//
//    public static String encodeToPasswordHex(User user, String password) throws Exception {
//        byte[] salt = EncodesUtils.decodeHex(user.getSalt());
//        byte[] hashLoginPwd = DigestUtils.sha1(password.getBytes("UTF-8"), salt,
//                HASH_INTERATIONS);
//        return EncodesUtils.encodeHex(hashLoginPwd);
//    }
    public static void encryptPassword(SysUserDTO user, String password) throws Exception {
        byte[] salt = DigestUtils.generateSalt(SALT_SIZE);
        user.setSalt(EncodesUtils.encodeHex(salt));
        byte[] hashLoginPwd = DigestUtils.sha1(password.getBytes("UTF-8"), salt,
                HASH_INTERATIONS);
        user.setPassword(EncodesUtils.encodeHex(hashLoginPwd));
    }

    public static String encodeToPasswordHex(SysUserDTO user, String password) throws Exception {
        byte[] salt = EncodesUtils.decodeHex(user.getSalt());
        byte[] hashLoginPwd = DigestUtils.sha1(password.getBytes("UTF-8"), salt,
                HASH_INTERATIONS);
        return EncodesUtils.encodeHex(hashLoginPwd);
    }
}
